---
title: HTTP cache
---


> This page focuses on the HTTP cache. If you want to deduplicate the storage of your objects and/or notify your UI when your data changes, take a look at the [normalized cache](./normalized-cache) instead.

<Note>

The HTTP cache is only available on Android and the JVM.

</Note>

## Setup

To enable HTTP cache support, add the dependency to your project's `build.gradle` file:

```kotlin title="build.gradle[.kts]"
dependencies {
  implementation("com.apollographql.apollo:apollo-http-cache:4.1.0")
}
```

If you're targeting Android API level < 26, you'll need to enable [core library desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) to support the `java.time` API:

```kotlin
android {
    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled = true
    }
}
```

Then configure your HTTP cache:

```kotlin
apolloClient = ApolloClient.Builder()
    .httpCache(
      // Use a dedicated directory for the cache
      directory = File(pathToCacheDirectory),
      // Configure a max size of 100MB
      maxSize = 100 * 1024 * 1024
    )
    .build()
```

## Usage

The HTTP cache is a Least Recently Used (LRU) cache with a configurable max size.

Once your cache setup is complete, the cache will be used by default by all your queries. By default, queries will try to find a result in the cache first and go the network if it's not there. This is the `HttpFetchPolicy.CacheFirst` policy. You can customize that behaviour with `httpFetchPolicy(HttpFetchPolicy)`:

```kotlin
val response = apolloClient.query(query)
  // Try the cache first - if it's a miss, try the network 
  .httpFetchPolicy(HttpFetchPolicy.CacheFirst)

  // Only use the cache
  .httpFetchPolicy(HttpFetchPolicy.CacheOnly)

  // Try the network first - if there's an error, try the cache 
  .httpFetchPolicy(HttpFetchPolicy.NetworkFirst)

  // Don't use the cache
  .httpFetchPolicy(HttpFetchPolicy.NetworkOnly)
  
  // Finally, execute your query
  .execute()
```
Note: mutations and subscriptions don't go through the cache.

If the query is present in cache, it will be used to return `response.data`. If not, a `HttpCacheMissException` will be thrown.

You can also set an expiration time either globally or for specific queries. The entries will automatically be removed from the cache after the expiration time:

```kotlin
// Globally
apolloClient = ApolloClient.Builder()
    .httpCache(/*...*/)
    // Expire after 1 hour
    .httpExpireTimeout(60 * 60 * 1000)
    .build()

// On a specific query
val response = apolloClient.query(query)
    // Expire after 1 hour
    .httpExpireTimeout(60 * 60 * 1000)
    .execute()
```

If a specific query must not be cached, you can use `httpDoNotStore()`:

```kotlin
val response = apolloClient.query(query)
    // Don't cache this query
    .httpDoNotStore(httpDoNotStore = true)
    .execute()
```

## Clearing the cache

Call `apolloClient.httpCache.clearAll()` to clear the cache of all entries.
